package com.view.cn.mvprxjava.presenter;

import com.view.cn.mvprxjava.callback.UserOnResponse;
import com.view.cn.mvprxjava.comm.RequestCodeComm;
import com.view.cn.mvprxjava.iView.UserView;
import com.view.cn.mvprxjava.model.UserModel;

/**
 * Created by Zengxiaoping on 2016/9/22.
 */
public class UserPresenter implements BasePresenter {

	UserModel mUserModel;
	UserView mUserView;

	public UserPresenter(UserView userView) {
		mUserView = userView;
		mUserModel = new UserModel();
	}

	public void getUserInfo(String userName, String userPwd) {
		if (!verificationUserInfo(userName, userPwd, mUserView)) {
			return;
		}

		mUserView.showDialog();
		mUserModel.getUserInfo(userName, userPwd, new UserOnResponse() {
			@Override
			public void userNameError(Object... o) {
				mUserView.userNameError(o);
				mUserView.hideDialog();
			}

			@Override
			public void userNameLengthError(Object... o) {
				mUserView.userNameLengthError(o);

			}

			@Override
			public void userInfoEmpty(Object... o) {
				mUserView.userInfoEmpty(o);
				mUserView.hideDialog();
			}

			@Override
			public void success(int requestCode, String url, String data) {
				mUserView.respense(requestCode, url, data);
				mUserView.hideDialog();
			}

			@Override
			public void fail(int requestCode, Exception e, String data) {
				mUserView.fail(requestCode, e, data);
				mUserView.hideDialog();
			}
		});
	}

	//验证账号密码
	public boolean verificationUserInfo(String userName, String userPwd, UserView userView) {
		if (userName == null || userName.equals("") || userPwd == null || userPwd.equals("")) {
			userView.userInfoEmpty(RequestCodeComm.USER_CODE_GET_USER_INFO, null, "用户账户或密码不能为空");
			return false;
		} else if (userName.length() < 6 || userPwd.length() < 6) {
			userView.userNameLengthError(RequestCodeComm.USER_CODE_GET_USER_INFO, null, "用户名或者密码长度未到6位");
			return false;
		}
		return true;
	}

	@Override
	public void onCreate() {

	}

	@Override
	public void onPause() {

	}

	@Override
	public void onResume() {

	}

	@Override
	public void onDestroy() {

	}
}
